package cc.linker.steplink.crm.merchanth5.service.common;

import cc.linker.steplink.crm.commons.constant.WcPayConstant;
import cc.linker.steplink.crm.commons.http.HttpService;
import cc.linker.steplink.crm.commons.model.WcPayProperties;
import cc.linker.steplink.crm.merchanth5.dao.acs.PurchaseProcessDao;
import cc.linker.steplink.crm.response.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SmsService {
    /**
     * 默认公众号token
     */
    private static final String DEFAULT_PUBLIC_ID = "gh_f5441cf537aa";

    @Autowired
    private HttpService httpService;
    @Autowired
    private WcPayProperties wcPayProperties;
    @Autowired
    private PurchaseProcessDao purchaseProcessDao;

    /**
     * 发消息
     */
    public String sendMsg(String orgId, Map<String, Object> param) {
        return this.post(orgId, WcPayConstant.SMS_SEND_MESSAGE, param);
    }

    /**
     * 发验证码
     */
    public String sendVerification(String orgId, String phone) {
        Map<String, Object> param = new HashMap<>();
        param.put("businessType", "1020");
        param.put("mobilNumber", phone);
        param.put("params", "");
        param.put("token", "");
        return this.sendVerification(orgId, param);
    }

    /**
     * 发验证码
     */
    public String sendVerification(String orgId, Map<String, Object> param) {
        return this.post(orgId, WcPayConstant.SEND_VERIFICATION_CODE, param);
    }

    /**
     * 验证验证码
     */
    public String checkVerification(String orgId, String phone, String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("businessType", "1020");
        param.put("mobilNumber", phone);
        param.put("randomCode", code);
        return this.checkVerification(orgId, param);
    }

    /**
     * 验证验证码
     */
    public String checkVerification(String orgId, Map<String, Object> param) {
        return this.post(orgId, WcPayConstant.CHECK_VERIFICATION_CODE, param);
    }

    /**
     * @param orgId 机构id或token
     */
    private String post(String orgId, String suffix, Map<String, Object> param) {
        String url = wcPayProperties.getSmsurl() + suffix;
        String token;
        if (StringUtils.isEmpty(orgId)) {
            throw GlobalException.build("缺少机构信息");
        }
        if (orgId.length() == 5) {
            token = purchaseProcessDao.getTokenByOrgId(orgId);
        } else {
            token = orgId;
        }
        List<String> smsTokenList = purchaseProcessDao.getSmsTokenList();
        if (smsTokenList == null) {
            throw GlobalException.build("缺少短信配置");
        }
        if (smsTokenList.contains(token)) {
            param.put("plantName", token);
        } else {
            param.put("plantName", DEFAULT_PUBLIC_ID);
        }
//        boolean flag = false;
//        if (orgId.length() == 5) {
//            String token = purchaseProcessDao.getTokenByOrgId(orgId);
//            if (PUBLIC_ID.equals(token)) {
//                flag = true;
//            }
//        } else if (PUBLIC_ID.equals(orgId)) {
//            flag = true;
//        }
//        if (flag) {
//            param.put("plantName", XLJS);
//        } else {
//            param.put("plantName", STEPLINK);
//        }
//
        String result = null;
        try {
            result = httpService.doPost(url, param);
        } catch (URISyntaxException | IOException e) {
            LOGGER.error("短信网关调用异常");
        }
        return result;
    }
}
